﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.ServiceProject;

namespace ZBJF.Warranty.BLL
{
    public partial class ServiceProviderProductService
    {
        #region 获取单个服务商产品 + ServiceProduct LoadByID(int id)
        /// <summary>
        /// 获取单个服务商产品
        /// </summary>
        /// <param name="id">清洗产品ID</param>
        /// <returns></returns>
        public ServiceProviderProduct LoadByID(int id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }
        #endregion

        #region 判断是否存在 + bool IsExist(int serviceId, string serviceProductName, int id = 0)
        /// <summary>
        /// 判断是否存在
        /// </summary>
        /// <param name="serviceProviderId">服务商ID</param>
        /// <param name="thirdProductName">服务商产品名称</param>
        /// <param name="id">需要排除的服务商产品ID</param>
        /// <returns></returns>
        public bool IsExist(int serviceProviderId, string thirdProductName, int id = 0)
        {
            var expressionWhere = PredicateExtensions.True<ServiceProviderProduct>();
            expressionWhere = expressionWhere.And(o => o.ServiceProviderId == serviceProviderId);
            expressionWhere = expressionWhere.And(o => o.ThirdProductName == thirdProductName);
            if (id > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ID != id);
            }
            return LoadEntities(expressionWhere).Any();
        }
        #endregion

        #region 分页查询 + IQueryable<ServiceProduct> GetPagingList(ServiceProductModel model, out int total)
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="model">过滤条件</param>
        /// <param name="total">返回总条数</param>
        /// <returns></returns>
        public List<ServiceProviderProductViewModel> GetPagingList(ServiceProviderProductModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<ServiceProviderProduct>();
            //服务商ID
            if (model.ServiceProviderId > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ServiceProviderId == model.ServiceProviderId);
            }
            //服务商产品名称
            if (!string.IsNullOrEmpty(model.ServiceProviderProductName))
            {
                expressionWhere = expressionWhere.And(o => o.ThirdProductName.Contains(model.ServiceProviderProductName));
            }

            var expressionWhere1 = PredicateExtensions.True<ServiceProject>();
            //中保产品名称
            if (!string.IsNullOrEmpty(model.ServiceProjectName))
            {
                expressionWhere1 = expressionWhere1.And(o => o.ProjectName.Contains(model.ServiceProjectName));
            }

            var result = (from a in LoadPagerEntities<ServiceProviderProduct>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts).AsNoTracking()
                          join b in ServiceFactory.ServiceProjectServiceInstance.LoadEntities(expressionWhere1) on a.ServiceProjectId equals b.ID
                          join d in ServiceFactory.ServiceProviderServiceInstance.LoadEntities(t => true) on a.ServiceProviderId equals d.ID into temp1
                          from e in temp1.DefaultIfEmpty()
                          select new ServiceProviderProductViewModel
                          {
                              ID = a.ID,
                              IsAvailable = a.IsAvailable,
                              LastModifyBy = a.LastModifyBy,
                              LastModifyTime = a.LastModifyTime,
                              Price = b.Price,
                              ProjectName = b.ProjectName,
                              ServiceProviderName = e.ServiceProviderName,
                              ThirdProductName = a.ThirdProductName,
                              ThirdProductPrice = a.ThirdProductPrice
                          }).ToList();
            return result;
        }
        #endregion

    }
}
